import React, { Component} from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataTableCreate from '../../addons/DataTable'
import dataEditCreate from '../../addons/DataEdit'
import * as menuService from '../../../services/menus';
import * as common from '../../../utils/common';
import COMP_QTZB_DIVI_FHQK from './COMP_QTZB_DIVI_FHQK'

const daoName = "CompCmFbblc";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
  { title: '数据日期', width: 110, sorter: true, caption: 'SJRQ', key: 'SJRQ', dispaly: true, filter: true,render: data => datetime.format(data.SJRQ, "yyyy-MM-dd")  },
  { title: '客户名称', width: 250, sorter: true, nowrap: true ,caption:'KHMC', key: 'KHMC', dispaly: true, filter: true },
  { title: '组织机构号', width: 130, sorter: true, dataIndex: 'Zjhm', key: 'Zjhm', dispaly: true, filter: true },
  { title: '所属机构', width: 190, sorter: true, dataIndex: 'Ssjg', key: 'Ssjg', dispaly: true, filter: true },
  { title: '币种', width: 110, sorter: true, key: 'CYNM', dataIndex: "CYNM", dispaly: true, filter: true },
  { title: '起息日', width: 110, sorter: true, key: 'Qxr',caption: "Qxr", dispaly: true, filter: true ,render: data => datetime.format(data.Qxr, "yyyy-MM-dd")},
  { title: '到期日', width: 110, sorter: true, key: 'Dqr',caption: "Dqr", dispaly: true, filter: true ,render: data => datetime.format(data.Dqr, "yyyy-MM-dd")},
  { title: '产品名称', width: 130, sorter: true, nowrap: true ,caption:'Cpmc', key: 'Cpmc', dispaly: true, filter: true },
  { title: '产品代码', width: 140, sorter: true, dataIndex: 'Cpdm', key: 'Cpdm', dispaly: true, filter: true },
  { title: '客户收益率', width: 130, sorter: true, dataIndex: 'Khsyl', key: 'Khsyl', dispaly: true, filter: true },
  { title: '当月发生额', width: 140, sorter: true, dataIndex: 'Yfse', key: 'Yfse', dispaly: true, filter: true },
  { title: '当前余额', width: 140, sorter: true, dataIndex: 'CURRBAL', key: 'CURRBAL', dispaly: true, filter: true },
  { title: '客户经理1', width: 100, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
  { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption:'ZB1',render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
  { title: '客户经理2', width: 100, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
  { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption:'ZB2',render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
  { title: '客户经理3', width: 100, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
  { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption:'ZB3',render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
  { title: '客户经理4', width: 100, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
  { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption:'ZB4',render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
  { title: '客户经理5', width: 100, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
  { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption:'ZB5',render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
  { name: 'SJRQ', required: true, type: 'date', label: '数据日期', grid: 12 / 24 },
  { name: 'KHMC', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: "Zjhm", required: true, type: "text", label: "组织机构号", grid: 12 / 24},
  { name: "Ssjg", required: true, type: "text", label: "所属机构", grid: 12 / 24},
  { name: "CYNM", required: true, type: "text", label: "币种", grid: 12 / 24},
  { name: 'Qxr', required: true, type: 'date', label: '起息日', grid: 12 / 24 },
  { name: 'Dqr', required: true, type: 'date', label: '到期日', grid: 12 / 24 },
  { name: 'Cpmc', required: true, type: 'text', label: '产品名称', grid: 12 / 24 },
  { name: 'Cpdm', required: true, type: 'text', label: '产品代码', grid: 12 / 24 },
  { name: 'Khsyl', required: true, type: 'number', label: '客户收益率', grid: 12 / 24 },
  { name: 'Yfse', required: true, type: 'number', label: '当月发生额', grid: 12 / 24 },
  { name: 'CURRBAL', required: true, type: 'number', label: '当前余额', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
/*对私理财产品*/
class COMP_FBBLCForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID" , permID: this.props.permID}
      
    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });
    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showModelHandler(isAdd) {
      let key = null
      if (!isAdd) {
          const len = this.props.selectedRowKeys.length;
          if (len === 0) {
              message.info('请选择要认定的数据行！');
              return;
          }
          key = this.props.selectedRowKeys[len - 1]
      }
      this.props.dispatch({
          type: `${daoName}/openEdit`,
          payload: { key }
      });
  }
  render() {
    let { showEdit, record, permID, popedomTypes } = this.props;
    let pTypes= popedomTypes[permID]
    let data =[]
    if (pTypes.indexOf("6")!==-1){
        data.push({ value: '2', label: '业绩认定', onClick: this.showModelHandler.bind(this, false) })
    }
    const controlsFilter = [
      { name: 'CX', required: false, type: 'button-group', grid: 5 / 24, data: data, grid: 5 / 24 },
    ];
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          {/* <div className={styles.create}>
              <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} />
          </div> */}
          <COMP_QTZB_DIVI_FHQK ref="divi" jlid={record.ID} ywid={8} title={"【" + record.KHMC + "】 业绩分配"} showEdit={showEdit} parentModel={daoName} />
          <DataTable ref="fapuTable" daoName={daoName} columns={columns} controls={controls} permID={permID}/>
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes} =state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
COMP_FBBLCForm = connect(mapStateToProps)(COMP_FBBLCForm)
export default COMP_FBBLCForm;
let render = (props) => React.createElement(COMP_FBBLCForm, props);
let f = new Factory()
f.set("COMP_FBBLCForm", render)